﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Model.ViewModel;
using KnowledgeCommunity.Service;
using System.IO;

namespace KnowledgeCommunity.Web.DbHelper
{
    public class UserAccountHelper
    {

        CKCDB mydb = new CKCDB();
        /// <summary>
        /// FindAll()
        /// </summary>
        /// <returns></returns>
        public IEnumerable<UserAccount> FindAll()
        {
            var apps = from n in mydb.UserAccounts
                       orderby n.AddTime descending
                       select n;
            return apps;
        }

        public IEnumerable<UserAccount> NewUserAccount(int count)
        {
            var apps = (from n in mydb.UserAccounts
                        orderby n.AddTime descending
                        select n).Take(count);
            return apps;
        }

        /// <summary>
        /// FindById(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserAccount FindById(int id)
        {
            var app = mydb.UserAccounts.Find(id);
            return app;
        }
        /// <summary>
        /// FindByUserName
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public UserAccount FindByUserName(string username)
        {
            var apps = (from n in mydb.UserAccounts
                       where n.UserName == username
                       orderby n.AddTime descending
                       select n).FirstOrDefault();
            return apps;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<UserModel> FindTheRankingUser()
        {
            var modelList = new List<UserModel>();
            var user = (from u in mydb.Knowledges select u.UserName).Distinct();
            foreach (var currentUser in user)
            {
                UserModel currentModel = new UserModel();
                int count1 = (from k in mydb.Knowledges where k.UserName == currentUser select k).Count();
                int count2 = (from k in mydb.Comments where k.UserName == currentUser select k).Count();
                int count3 = (from k in mydb.UserAccounts where k.UserName == currentUser select k).Count();
                UserAccount _user = FindByUserName(currentUser);
                currentModel.KonwledgeCount = count1;
                currentModel.CommentCount = count2;
                currentModel.CommidityCount = count3;
                currentModel.Name = currentUser;
                if (_user.ImgUrl != "" || _user.ImgUrl != null)
                {
                    currentModel.Logo = _user.ImgUrl;
                }
                else 
                {
                    currentModel.Logo = "Default";
                }
                modelList.Add(currentModel);
            }

            return modelList;
        }


        public IEnumerable<UserAccount> SearchByKeyAndTime(string key, int role, DateTime stime, DateTime etime)
        {
            IEnumerable<UserAccount> apps = null;
            DateTime start = Convert.ToDateTime("2012-1-2");
            DateTime end = Convert.ToDateTime("2100-12-30");
            if (role == 0) 
            {
                role = 1;
            }

            if (key == "" && stime < start && etime > end)   //0<>
            {
                apps = FindAll();
            }
            else if (key == "" && stime < start && etime > end)   //0>>
            {
                apps = from n in mydb.UserAccounts
                       where n.AddTime >= Convert.ToDateTime(stime) && n.Role == role
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == "" && stime < start && etime > end)   //0<<
            {
                apps = from n in mydb.UserAccounts
                       where n.AddTime <= Convert.ToDateTime(etime) && n.Role == role
                       orderby n.AddTime descending
                       select n;
            }
            else if (key == null && stime < start && etime > end)   //0><
            {
                apps = from n in mydb.UserAccounts
                       where n.AddTime >= Convert.ToDateTime(stime) && n.AddTime <= Convert.ToDateTime(etime) && n.Role == role
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != "" && stime < start && etime > end)   //1<>
            {
                apps = from n in mydb.UserAccounts
                       where n.UserName.Contains(key) && n.Role == role
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != "" && stime < start && etime > end)   //1>>
            {
                apps = from n in mydb.UserAccounts
                       where n.UserName.Contains(key) && n.AddTime >= Convert.ToDateTime(stime) && n.Role == role
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != "" && stime < start && etime > end)   //1<<
            {
                apps = from n in mydb.UserAccounts
                       where n.UserName.Contains(key) && n.AddTime <= Convert.ToDateTime(etime) && n.Role == role
                       orderby n.AddTime descending
                       select n;
            }
            else if (key != "" && stime < start && etime > end)   //1><
            {
                apps = from n in mydb.UserAccounts
                       where n.UserName.Contains(key) && n.AddTime >= Convert.ToDateTime(stime) && n.AddTime <= Convert.ToDateTime(etime) && n.Role == role
                       orderby n.AddTime descending
                       select n;
            }
            return apps;
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">UserAccount</param>
        /// <returns></returns>
        public bool Create(UserAccount model)
        {
            try
            {
                mydb.UserAccounts.Add(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Create in table<UserAccounts>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">UserAccount</param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var model = FindById(id);
            string path = model.DelPath;
            try
            {
                mydb.UserAccounts.Remove(model);
                mydb.SaveChanges();
                File.Delete(path);
                return true;
            }
            catch (Exception ex)
            {
                string message = "Delete in table<UserAccounts>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }
    }
}